<template>
  <PanerForm :isShowBarDrawer="true" :caption="'编辑笔记'">
    <template #body>
      <div class="d-row">
        <div class="d-form-group d-form-group-flex">
          <div class="d-form-label d-text-right">
            <label class="label-text">标题</label>
          </div>
          <div class="d-form-el">
            <input v-model="state.title" class="d-form-input" type="text" />
          </div>
        </div>
        <div class="d-form-group d-form-group-flex">
          <div class="d-form-label d-text-right">
            <label class="label-text">内容</label>
          </div>
          <div class="d-form-el">
            <HtmlViewer v-model="state.content" :mode="STATUS.EDIT" ref="htmlViewer" />
          </div>
        </div>
        <div class="d-form-group d-form-group-flex d-col-xs-6">
          <div class="d-form-label d-text-right">
            <label class="label-text">类型</label>
          </div>
          <div class="d-form-el">
            <select v-model="state.type" class="d-form-select">
              <option v-for="item in noteTypes" :key="item.id" :value="item.id">{{ item.text }}</option>
            </select>
          </div>
        </div>
        <div class="d-form-group d-form-group-flex d-col-xs-6">
          <div class="d-form-label d-text-right">
            <label class="label-text">标签</label>
          </div>
          <div class="d-form-el">
            <input v-model="state.labels" class="d-form-input" type="text" />
          </div>
        </div>
        <FormCalendar
          v-model="state.storeExpire"
          label="存储期限"
          labelWidth="90px"
          format="yyyy-MM-dd HH:mm:ss"
          class="d-form-group-flex d-col-xs-6"
        ></FormCalendar>
        <FormCalendar
          v-if="isEdit"
          v-model="state.updateTime"
          :isEditAble="false"
          :readonly="true"
          format="yyyy-MM-dd HH:mm:ss"
          label="修改时间"
          labelWidth="90px"
          class="d-form-group-flex d-col-xs-6"
        ></FormCalendar>
        <div v-if="false && isEdit" class="d-form-group d-form-group-flex d-col-xs-6">
          <div class="d-form-label d-text-right">
            <label class="label-text">修改人</label>
          </div>
          <div class="d-form-el">
            <input v-model="state.updateUser" class="d-form-input" type="text" readonly />
          </div>
        </div>
        <FormCalendar
          v-if="isEdit"
          v-model="state.createTime"
          :isEditAble="false"
          :readonly="true"
          format="yyyy-MM-dd HH:mm:ss"
          label="创建时间"
          labelWidth="90px"
          class="d-form-group-flex d-col-xs-6"
        ></FormCalendar>
        <div v-if="false && isEdit" class="d-form-group d-form-group-flex d-col-xs-6">
          <div class="d-form-label d-text-right">
            <label class="label-text">创建人</label>
          </div>
          <div class="d-form-el">
            <input v-model="state.createUser" class="d-form-input" type="text" readonly />
          </div>
        </div>
      </div>
    </template>
    <template #bar-drawer>
      <button @click="handleSubmit" class="d-btn d-btn-primary" type="button">提交</button>
      <button v-if="!isEdit" @click="handleReset" class="d-btn d-btn-default" type="reset">重置</button>
      <a v-d-goback class="d-btn d-btn-default">返回</a>
    </template>
  </PanerForm>
</template>

<script>
import { PanerForm } from '@daelui/vdog/dist/pages'
import { Dialoger, FormCalendar } from '@daelui/vdog/dist/components'
import { HtmlViewer } from '@daelui/vdog/dist/viewer'
import { zh as locale } from '@daelui/dogjs/dist/locale'
import { STATUS } from '@daelui/dogjs/dist/constant'
import noteSer from '../../service/action/note.action.js'
import noteTypeSer from '../../service/action/noteType.action'

export default {
  components: {
    PanerForm,
    FormCalendar,
    HtmlViewer
  },

  data () {
    return {
      STATUS,
      isEdit: false, // 是否是编辑模式
      // 表单数据
      state: {
        id: '',
        title: '',
        content: '',
        contentItems: [],
        type: '',
        labels: '',
        storeExpire: '',
        updateTime: '',
        updateUser: '',
        createTime: '',
        createUser: ''
      },
      // 笔记类型
      noteTypes: []
    }
  },

  methods: {
    /**
     * @function 加载笔记
    */
    loadItem (id) {
      noteSer.queryItem({ id }).then(result => {
        let data = result.data || {}
        this.state = data
        data.content = data.content || ''
        // 替换所有的换行符
        data.content = data.content.replace(/\r\n/g, '<br />')
        data.content = data.content.replace(/\n/g, '<br />')
        this.$refs.htmlViewer.update(data.content)
      })
    },

    /**
     * @function 提交
    */
    handleSubmit () {
      let defer = null
      // 编辑
      if (this.isEdit) {
        defer = noteSer.editItem(this.state)
      } else { // 添加
        defer = noteSer.addItem(this.state)
      }
      defer.then(result => {
        if (result.success) {
          Dialoger.success(locale.action.success)
        } else {
          Dialoger.danger(locale.action.fail)
        }
        // 添加则回到列表页
        if (!this.isEdit) {
          this.$router.push(this.getMicroRoute('/note/list'))
        }
      })
    },
    /**
     * @function 重置
    */
    handleReset () {

    },

    /**
     * @function 添加内容项
    */
    handleAddContentItem () {
      this.state.contentItems.push({
        id: new Date().getTime(),
        type: 'textarea',
        value: ''
      })
    }
  },

  beforeMount () {
    let params = this.$route.params
    // 新建
    if (!params.id) {
      this.isEdit = false
    } else { // 编辑
      this.isEdit = true
      this.loadItem(params.id)
    }
    if (!this.state.contentItems.length) {
      this.state.contentItems.push({
        id: new Date().getTime(),
        type: 'textarea',
        value: ''
      })
    }
  },

  mounted () {
    // 查询笔记列表
    noteTypeSer.queryList().then(res => {
      let list = res.data
      list = Array.isArray(list) ? list : []
      this.noteTypes = list
    })
  }
}
</script>

<style lang="less" scoped>
.d-form-label {
  width: 90px;
}
.d-form-textarea {
  min-height: 320px;
}
.d-form-group /deep/.ql-container .ql-editor {
  min-height: 200px;
}
</style>
